<template>
  <div class="home-container">
    <!-- 顶部招聘宣传图轮播 -->
    <div class="promo-carousel-container">
      <div ref="promoCarouselWrapper" class="promo-carousel-wrapper">
        <div ref="promoCarouselTrack" class="promo-carousel-track">
          <!-- 左侧复制项 -->
          <div
              v-if="promoItems.length > 0"
              :key="'clone-prev'"
              class="promo-carousel-item"
          >
            <img :src="promoItems[promoItems.length - 1].imageUrl" alt="招聘宣传图" class="promo-image">
          </div>

          <!-- 真实图片 -->
          <div
              v-for="(promo, index) in promoItems"
              :key="index"
              class="promo-carousel-item"
          >
            <img :src="promo.imageUrl" alt="招聘宣传图" class="promo-image">
          </div>

          <!-- 右侧复制项 -->
          <div
              v-if="promoItems.length > 0"
              :key="'clone-next'"
              class="promo-carousel-item"
          >
            <img :src="promoItems[0].imageUrl" alt="招聘宣传图" class="promo-image">
          </div>
        </div>
      </div>

      <!-- 轮播控制按钮 -->
      <button
          class="carousel-control prev"
          @click="prevSlide"
          v-if="promoItems.length > 1"
      >
        <i class="fa fa-angle-left"></i>
      </button>
      <button
          class="carousel-control next"
          @click="nextSlide"
          v-if="promoItems.length > 1"
      >
        <i class="fa fa-angle-right"></i>
      </button>

      <!-- 轮播指示器 -->
      <div class="carousel-indicators" v-if="promoItems.length > 1">
        <button
            v-for="(item, index) in promoItems"
            :key="index"
            :class="{ active: currentSlide === index }"
            @click="goToSlide(index)"
        ></button>
      </div>
    </div>

    <!-- 下方精选职位列表 -->
    <div class="job-list-container">
      <h2 class="section-title">精选职位</h2>

      <div class="job-list-three-columns">
      <el-card v-for="(job, index) in jobList" :key="job.id" class="job-card" shadow="hover" @click="handleJobClick(job)" >
        <div class="job-header">
          <h3>{{ job.title }}</h3>
          <el-tag type="success" size="small">
            {{ job.positionType }}
          </el-tag>
        </div>
        <div class="job-salary">{{ job.salaryRange }}</div>
        <div class="job-meta">
          <span>{{ job.educationRequired }}</span>
          <span>{{ job.experienceRequired }}</span>
        </div>
        <div style="color: #af99ad;"><span>发布时间：{{ job.publishDate }}</span></div>
        <div class="company-info">
          <div class="company-name">{{ job.companyName }}</div>
          <div class="company-detail">
            {{ job.companyScale }}
          </div>
        </div>
      </el-card>
    </div>
    </div>
    <!-- 简洁版职位详情Dialog -->
    <el-dialog v-model="detailVisible" title="职位详情" width="600px" >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="企业名称">
          {{ positionDetail.companyName }}
        </el-descriptions-item>
        <el-descriptions-item label="职位名称">
          {{ positionDetail.title }}
        </el-descriptions-item>
        <el-descriptions-item label="薪资范围">
          {{ positionDetail.salaryRange }}
        </el-descriptions-item>
        <el-descriptions-item label="工作地点">
          {{ positionDetail.workLocation || '未填写' }}
        </el-descriptions-item>
        <el-descriptions-item label="经验要求">
          {{ positionDetail.experienceRequired }}
        </el-descriptions-item>
        <el-descriptions-item label="学历要求">
          {{ positionDetail.educationRequired }}
        </el-descriptions-item>
        <el-descriptions-item label="职位类型">
          {{ positionDetail.positionType }}
        </el-descriptions-item>
        <el-descriptions-item label="发布时间">
          {{ positionDetail.publishDate }}
        </el-descriptions-item>
      </el-descriptions>

      <div class="detail-block">
        <h4>职位描述</h4>
        <div class="content-box">{{ positionDetail.description || '无' }}</div>
      </div>

      <div class="detail-block">
        <h4>职位要求</h4>
        <div class="content-box">{{ positionDetail.requirements || '无' }}</div>
      </div>

      <template #footer>
        <el-button @click="chat(positionDetail.hrId)">立即沟通</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { indexApi, getPosByIdApi } from '../../api/jobSeeker';

const router = useRouter();
// 轮播图数据
const promoItems = ref([
  { imageUrl: '/lunbo.png' },
  { imageUrl: '/lunbo3.png' },
  { imageUrl: '/lunbo2.png' }
]);

// 轮播图状态
const currentSlide = ref(0);
const slideCount = ref(0);
const autoPlayInterval = ref(null);
const promoCarouselWrapper = ref(null);
const promoCarouselTrack = ref(null);

// 职位列表数据
const jobList = ref([]);

// 初始化轮播图
onMounted(() => {
  nextTick(() => {
    if (!promoItems.value.length) return;

    slideCount.value = promoItems.value.length;
    updateSlidePosition(1, false);
    startAutoPlay();
    window.addEventListener('resize', handleResize);
  });
  queryPos()
});
//查询职位
const queryPos = async () => {
  const result = await indexApi();
  if  (result) {
    jobList.value = result.data;
  } else {
    ElMessage.error(result.message);
  }
}
//职位详情dialog
const detailVisible = ref(false)
const positionDetail = ref({
  hrId: '1',
  companyName: '上海XXX科技有限公司',
  title: '发生士大夫士大夫',
  salaryRange: '2k-3k',
  workLocation: '手动阀手动阀手动阀阿嘎尔啊',
  experienceRequired: '手动阀手动阀手动阀阿嘎尔啊',
  educationRequired: '手动阀手动阀手动阀阿嘎尔啊',
  positionType: '萨法',
  publishDate: '2022-05-05',
  description: '手动阀手动阀手动阀阿嘎尔啊手动阀手动阀手动阀阿嘎尔啊',
  requirements: '...'
})
//通职位id查询职位详情
const handleJobClick = async (job) => {
  detailVisible.value = true
  const result = await getPosByIdApi(job.id)
  if(result.code){
    positionDetail.value = result.data
  }else{
    ElMessage.error(result.msg)
  }
}
const chat = (hrId) => {
  // 将HR ID存储在本地存储中，以便聊天页面获取
  localStorage.setItem('targetHrId', hrId)
  router.push('/jobseeker/umenu3')
}
// 清理资源
onUnmounted(() => {
  stopAutoPlay();
  window.removeEventListener('resize', handleResize);
});

// 轮播图控制
const updateSlidePosition = (position, withTransition = true) => {
  const slideWidth = promoCarouselWrapper.value?.clientWidth || 0;
  if (!slideWidth) return;

  promoCarouselTrack.value.style.transition = withTransition ? 'transform 0.5s ease' : 'none';
  promoCarouselTrack.value.style.transform = `translateX(-${position * slideWidth}px)`;
};

const prevSlide = () => {
  stopAutoPlay();

  let newSlide = currentSlide.value - 1;

  if (newSlide < 0) {
    updateSlidePosition(slideCount.value + 1);
    setTimeout(() => {
      updateSlidePosition(slideCount.value, false);
    }, 500);
    currentSlide.value = slideCount.value - 1;
  } else {
    currentSlide.value = newSlide;
    updateSlidePosition(currentSlide.value + 1);
  }

  startAutoPlay();
};

const nextSlide = () => {
  stopAutoPlay();

  let newSlide = currentSlide.value + 1;

  if (newSlide >= slideCount.value) {
    updateSlidePosition(0);
    setTimeout(() => {
      updateSlidePosition(1, false);
    }, 500);
    currentSlide.value = 0;
  } else {
    currentSlide.value = newSlide;
    updateSlidePosition(currentSlide.value + 1);
  }

  startAutoPlay();
};

const goToSlide = (index) => {
  stopAutoPlay();
  currentSlide.value = index;
  updateSlidePosition(currentSlide.value + 1);
  startAutoPlay();
};

const startAutoPlay = () => {
  if (slideCount.value <= 1) return;

  stopAutoPlay();
  autoPlayInterval.value = setInterval(nextSlide, 5000);
};

const stopAutoPlay = () => {
  if (autoPlayInterval.value) {
    clearInterval(autoPlayInterval.value);
    autoPlayInterval.value = null;
  }
};

const handleResize = () => {
  updateSlidePosition(currentSlide.value + 1, false);
};
</script>

<style scoped>
.home-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

/* 轮播图样式 */
.promo-carousel-container {
  position: relative;
  height: 450px;
  margin: 10px 0 20px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.promo-carousel-track {
  display: flex;
  height: 100%;
}

.promo-carousel-item {
  flex: 0 0 100%;
  height: 100%;
}

.promo-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  cursor: pointer;
  transition: all 0.3s;
}

.carousel-control:hover {
  background-color: rgba(0, 0, 0, 0.7);
  transform: translateY(-50%) scale(1.1);
}

.carousel-control.prev {
  left: 20px;
}

.carousel-control.next {
  right: 20px;
}

.carousel-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.carousel-indicators button.active {
  width: 30px;
  border-radius: 5px;
}

/* 职位列表样式 */
 /* 每行4列的职位列表 */
.job-list-three-columns {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* 4列布局 */
  gap: 20px;
}

.job-card {
  padding: 20px;
  border-radius: 8px;
  transition: all 0.3s;
  background-color: #fff;
  border: 1px solid #eee;
  height: 100%; /* 确保卡片高度一致 */
  box-sizing: border-box; /* 包含padding和border在内的高度计算 */
}

.job-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.job-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.job-header h3 {
  font-size: 18px;
  margin: 0;
  color: #333;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.job-salary {
  color: #606eff;
  font-weight: bold;
  font-size: 16px;
  margin: 10px 0;
}

.job-meta {
  display: flex;
  gap: 15px;
  color: #666;
  font-size: 14px;
  margin: 15px 0;
}

.job-meta i {
  margin-right: 5px;
}

.company-info {
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px dashed #eee;
}

.company-name {
  font-weight: bold;
  margin-bottom: 5px;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.company-detail {
  font-size: 13px;
  color: #888;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .job-list-three-columns {
    grid-template-columns: repeat(2, 1fr); /* 屏幕较小时改为2列 */
  }
}

@media (max-width: 768px) {
  .job-list-three-columns {
    grid-template-columns: 1fr; /* 手机端改为1列 */
  }
}
.section-title {
  font-size: 28px;
  color: #1a1a1a;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 3px solid #1a73e8;
  text-align: left;
}
.detail-block {
  margin-top: 20px;
}

.detail-block h4 {
  margin-bottom: 8px;
  color: var(--el-text-color-primary);
}

.content-box {
  padding: 12px;
  border: 1px solid var(--el-border-color-light);
  border-radius: 4px;
  background-color: var(--el-fill-color-lighter);
  min-height: 60px;
}
</style>